package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.common.service.utils.AuthContextUtil;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/index")
@Tag(name="登录接口管理")
public class IndexController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("/login")
    @Operation(summary = "用户登录方法")
    @Parameters(@Parameter(name="loginDto",description = "登录请求对象",required = true))
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.selectUserByUsername(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    // com.atguigu.spzx.manager.controller.IndexController
    @GetMapping(value = "/getCode")
    @Operation(summary = "生成图片验证码方法")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping("userinfo")
    @Operation(summary = "从线程内获取用户信息方法")
    public  Result<SysUser> userInfo(){
//        SysUser user=sysUserService.getSysUserInfo(token);
        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
    }

    @GetMapping("logout")
    @Operation(summary = "退出时根据删除redis用户信息")
    @Parameters(@Parameter(name ="token",description = "请求头里的token",required = true))
    public Result logout(@RequestHeader String token){
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @GetMapping("getSysUserMenu")
    @Operation(summary = "根据用户id获取菜单信息")
    public Result getSysUserMenu(){
      List<SysMenuVo> list= sysMenuService.getSysUserMenu();
      return Result.build(list,ResultCodeEnum.SUCCESS);
    }
}
